package com.tebiecloud.goods.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.goods.client.model.entity.GoodsNotice;
import com.tebiecloud.goods.server.service.GoodsNoticeService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 *  前端控制器
 * @author tebie
 * @date 2019-10-28
 */
@RestController
@RequestMapping("/goodsnotice")
public class GoodsNoticeController {

    @Autowired
    private GoodsNoticeService goodsNoticeService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<GoodsNotice>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        GoodsNotice query = pageParams.mapToObject(GoodsNotice.class);
        QueryWrapper<GoodsNotice> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(goodsNoticeService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<GoodsNotice> get(@RequestParam("id") Long id) {
        GoodsNotice entity = goodsNoticeService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param goodsId 商品id,
     * @param effectiveTime 有效时间,
     * @param exceptTime 除什么时间外,
     * @param usageTime 使用时间,
     * @param reservation 预约信息,
     * @param applicableCrowd 适用人群,
     * @param tips 温馨提示,
     * @param deleted 删除状态：0没有删除1已被删除
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "goodsId", required = false) Long goodsId,
        @RequestParam(value = "effectiveTime", required = false) String effectiveTime,
        @RequestParam(value = "exceptTime", required = false) String exceptTime,
        @RequestParam(value = "usageTime", required = false) String usageTime,
        @RequestParam(value = "reservation", required = false) String reservation,
        @RequestParam(value = "applicableCrowd", required = false) String applicableCrowd,
        @RequestParam(value = "tips", required = false) String tips,
        @RequestParam(value = "deleted", required = false) Integer deleted
    ) {
        GoodsNotice entity = new GoodsNotice();
        entity.setGoodsId(goodsId);
        entity.setEffectiveTime(effectiveTime);
        entity.setExceptTime(exceptTime);
        entity.setUsageTime(usageTime);
        entity.setReservation(reservation);
        entity.setApplicableCrowd(applicableCrowd);
        entity.setTips(tips);
        entity.setDeleted(deleted);
        goodsNoticeService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param goodsId 商品id
     * @param effectiveTime 有效时间
     * @param exceptTime 除什么时间外
     * @param usageTime 使用时间
     * @param reservation 预约信息
     * @param applicableCrowd 适用人群
     * @param tips 温馨提示
     * @param deleted 删除状态：0没有删除1已被删除
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "goodsNoticeId", required = false) Long goodsNoticeId,
            @RequestParam(value = "goodsId", required = false) Long goodsId,
            @RequestParam(value = "effectiveTime", required = false) String effectiveTime,
            @RequestParam(value = "exceptTime", required = false) String exceptTime,
            @RequestParam(value = "usageTime", required = false) String usageTime,
            @RequestParam(value = "reservation", required = false) String reservation,
            @RequestParam(value = "applicableCrowd", required = false) String applicableCrowd,
            @RequestParam(value = "tips", required = false) String tips,
            @RequestParam(value = "deleted", required = false) Integer deleted
    ) {
        GoodsNotice entity = new GoodsNotice();
        entity.setGoodsNoticeId(goodsNoticeId);
        entity.setGoodsId(goodsId);
        entity.setEffectiveTime(effectiveTime);
        entity.setExceptTime(exceptTime);
        entity.setUsageTime(usageTime);
        entity.setReservation(reservation);
        entity.setApplicableCrowd(applicableCrowd);
        entity.setTips(tips);
        entity.setDeleted(deleted);
        goodsNoticeService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            goodsNoticeService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            goodsNoticeService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
